<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户姓名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="身份证号码" prop="idCard">
      <el-input v-model="dataForm.idCard" placeholder="身份证号码"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="是否有效" prop="active">
      <el-input v-model="dataForm.active" placeholder="是否有效"></el-input>
    </el-form-item>
    <el-form-item label="录入人" prop="creator">
      <el-input v-model="dataForm.creator" placeholder="录入人"></el-input>
    </el-form-item>
    <el-form-item label="录入时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="录入时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="modifier">
      <el-input v-model="dataForm.modifier" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userName: '',
          idCard: '',
          phone: '',
          active: '',
          creator: '',
          createTime: '',
          modifier: '',
          modifyTime: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证号码不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          active: [
            { required: true, message: '是否有效不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '录入人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '录入时间不能为空', trigger: 'blur' }
          ],
          modifier: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tb/customer/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.customer.userName
                this.dataForm.idCard = data.customer.idCard
                this.dataForm.phone = data.customer.phone
                this.dataForm.active = data.customer.active
                this.dataForm.creator = data.customer.creator
                this.dataForm.createTime = data.customer.createTime
                this.dataForm.modifier = data.customer.modifier
                this.dataForm.modifyTime = data.customer.modifyTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tb/customer/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userName': this.dataForm.userName,
                'idCard': this.dataForm.idCard,
                'phone': this.dataForm.phone,
                'active': this.dataForm.active,
                'creator': this.dataForm.creator,
                'createTime': this.dataForm.createTime,
                'modifier': this.dataForm.modifier,
                'modifyTime': this.dataForm.modifyTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
